package com.mew.common.dc.db;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mew.common.config.RedisConstantValue;
import com.mew.common.dc.mapper.GoldRecordMapper;
import com.mew.common.dc.mapper.LevelVipMapper;
import com.mew.common.dc.redis.UserProfileRedisService;
import com.mew.common.enm.DataStateType;
import com.mew.common.enm.GoldProcessType;
import com.mew.common.entity.config.LevelVip;
import com.mew.common.entity.record.GoldRecord;
import com.mew.common.entity.user.Profile;
import com.mew.common.vo.vip.VipUp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Date 2021/1/25 0025
 **/
@Service
@Slf4j
public class LevelConfigService {

    @Autowired
    private LevelVipMapper levelVipMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private UserProfileRedisService userProfileRedisService;
    @Autowired
    private ProfileService profileService;
    @Autowired
    private GoldRecordMapper goldRecordMapper;

    public static double rate = 1;//转化率

    public void loadLevel() throws Exception {

        LambdaQueryWrapper<LevelVip> vipWrapper = new LambdaQueryWrapper<LevelVip>()
                .eq(LevelVip::getState, DataStateType.Active.code());

        List<LevelVip> levelVipList = levelVipMapper.selectList(vipWrapper);
        if (null == levelVipList || levelVipList.isEmpty()){
            throw new Exception("----levelVipList is null --------");
        }

        levelVipList.stream().forEach(levelVip -> redisTemplate.opsForHash().put(RedisConstantValue.LEVEL_VIP, levelVip.getId(), levelVip));

    }

    public void updateLevelVip(LevelVip levelVip){
        LevelVip db = levelVipMapper.selectById(levelVip.getId());
        if (db == null){
            levelVipMapper.insert(levelVip);
        }else {
            levelVipMapper.updateById(levelVip);
        }
        redisTemplate.opsForHash().put(RedisConstantValue.LEVEL_VIP, levelVip.getId(), levelVip);
    }

    public LevelVip getLevelVipConfig(int levelVip){
        Object o = redisTemplate.opsForHash().get(RedisConstantValue.LEVEL_VIP, levelVip);

        return (LevelVip) o;
    }

    public LevelVip getLevelVipByUid(long userId){
        Profile profile = userProfileRedisService.getRedisProfileById(userId);
        return getLevelVipConfig(profile.getVipLevel());
    }

    public List<LevelVip> getAllLevelVipConfig(){
        List<LevelVip> list = redisTemplate.opsForHash().values(RedisConstantValue.LEVEL_VIP).stream().map(o -> (LevelVip) o).collect(Collectors.toList());

        return list;
    }

    /**
     *
     * exp 获得经验
     *
     * true 升级
     * @return
     */
    public VipUp vipLevelUp(long uid, long money,long j){

        long exp = (long) (money*rate);

        Profile profile = userProfileRedisService.getRedisProfileById(uid);
        Long oldJewe = profile.getJewelNumber();
        Integer vipLevel = profile.getVipLevel();
        Long vipExpValue = profile.getVipExpValue();
        LevelVip levelVip = getLevelVipConfig(vipLevel);

        Profile dbPro = Profile.builder().userId(uid).build();

        dbPro.setJewelNumber(j);
        profile.setJewelNumber(profile.getJewelNumber()+j);

        VipUp vipUp = new VipUp();

        if (vipExpValue+exp>levelVip.getExp()){
            //升级奖励
            LevelVip LevelVipUp = getLevelVipConfig(vipLevel+1);
            long levelUpGold = LevelVipUp.getLevelUpGold();
            vipUp.setLevelUp(true);
            vipUp.setLevel(vipLevel+1);
            //升级
            profile.setVipLevel(vipLevel+1);
            profile.setGoldNumber(profile.getGoldNumber()+levelUpGold);
            profile.setVipExpValue(0l);

            dbPro.setVipLevel(vipLevel+1);
            dbPro.setVipExpValue(0l);
            dbPro.setGoldNumber(levelUpGold);
            profileService.updateProfile(profile,dbPro, GoldProcessType.VIPUp);
        }else {
            //不升级
            vipUp.setLevel(vipLevel);
            profile.setVipExpValue(exp+vipExpValue);
            dbPro.setVipExpValue(exp+vipExpValue);
            profileService.updateProfile(profile,dbPro);
        }

        GoldRecord goldRecord = GoldRecord.builder()
                .userId(profile.getUserId())
                .processNumber(j)
                .processType(GoldProcessType.Jewe.code())
                .goldNumber(oldJewe)
                .build();
        goldRecordMapper.insert(goldRecord);

        return vipUp;
    }



}
